package pl.un4giv3n.mightvsmagic.action;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import pl.un4giv3n.mightvsmagic.entity.Action;
import pl.un4giv3n.mightvsmagic.entity.ActionType;
import pl.un4giv3n.mightvsmagic.entity.ActivityType;
import pl.un4giv3n.mightvsmagic.entity.User;
import pl.un4giv3n.mightvsmagic.entity.UserActivity;
import pl.un4giv3n.mightvsmagic.utils.ActionUtil;
import pl.un4giv3n.mightvsmagic.utils.ActivityLogUtil;

/**
 * @author andrzejewskia
 * go-to-work-jsp page action
 */
public class WorkPageAction extends PersistenceAction {

	private static final long serialVersionUID = 1L;
	/**
	 * session user
	 */
	private User user;
	private boolean showResult;
	private Action currentAction;

	/* (non-Javadoc)
	 * @see pl.un4giv3n.mightvsmagic.action.PersistenceAction#execute()
	 */
	@Override
	public String execute() {
		EntityManager entityManager = getEntityManager();
		user = getSessionUser(entityManager);
		if (user == null) {
			addActionError("Zaloguj się");
			entityManager.close();
			return LOGIN;
		}
		currentAction = user.getCurrentAction();
		if (currentAction != null){
			ActionType actionType = currentAction.getActionType();
			if(actionType.equals(ActionType.FIGHT)) {
				if (ActionUtil.actionEnded(currentAction, user)) {
					EntityTransaction transaction = entityManager
							.getTransaction();
					transaction.begin();
					user.setCurrentAction(Action.NO_ACTION);
					entityManager.merge(user);
					entityManager.flush();
					transaction.commit();
					currentAction = Action.NO_ACTION;
				}
			}else if (actionType == ActionType.WORK && ActionUtil.actionEnded(currentAction, user)) {
				entityManager.getTransaction().begin();
				user.addGold(currentAction.getReward().getRewardedGold());
				user.addFightExp(currentAction.getReward().getRewardedFightExp());
				user.addMagicExp(currentAction.getReward().getRewardedMagicExp());
				user.setCurrentAction(Action.NO_ACTION);
				StringBuilder msg = new StringBuilder("Zdobyte doświadczenie bitewne=");
				msg.append(currentAction.getReward().getRewardedFightExp());
				msg.append(", doświadczenie magiczne=").append(currentAction.getReward().getRewardedMagicExp());
				msg.append(", otrzymane złoto="+currentAction.getReward().getRewardedGold());
				UserActivity activity = ActivityLogUtil.createActivity(ActivityType.WORK_END, user.getId(), msg.toString());
				entityManager.persist(activity);
				entityManager.merge(user);
				entityManager.flush();
				entityManager.getTransaction().commit();
				entityManager.close();
				showResult = true;
				return ACTION_END;
			}
		}
		entityManager.close();
		if (currentAction == null || currentAction.equals(Action.NO_ACTION)) {
			return SUCCESS;
		} else {
			addActionError(ActionUtil.generateErrorMessage(currentAction, user));
			return INPUT;
		}
	}
	public User getUser() {
		return user;
	}
	public boolean isShowResult() {
		return showResult;
	}

	public void setShowResult(boolean showResult) {
		this.showResult = showResult;
	}
	
//data for the jsp
	
	public long getGold() {
		return currentAction.getReward().getRewardedGold();
	}

	public long getFightExp() {
		return currentAction.getReward().getRewardedFightExp();
	}

	public long getMagicExp() {
		return currentAction.getReward().getRewardedMagicExp();
	}
	
	public String getActionEnd() {
		return ActionUtil.generateStartMessage(currentAction, user);
	}
	
	public String getActionEndString(){
		return ActionUtil.getActionEndDate(currentAction, user);
	}
}
